package com.suke.czx.modules.contract.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.suke.czx.common.utils.FieldConstant;
import com.suke.czx.common.utils.R;
import com.suke.czx.modules.contract.entity.Contract;
import com.suke.czx.modules.contract.entity.ContractSearchBo;
import com.suke.czx.modules.contract.mapper.ContractMapper;
import com.suke.czx.modules.contract.service.ContractService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 应用模块名称: 合同
 * 代码描述: 合同业务层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/07 20:58:20
 */
@Service
@AllArgsConstructor
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {

    private final ContractMapper contractMapper;

    /**
     * 分页查询合同列表
     * @param contractSearchBo 查询BO
     * @return VO
     */
    @Override
    public R pageContractList(ContractSearchBo contractSearchBo) {
        Page<Contract> page = PageHelper.startPage(contractSearchBo.getPage(), contractSearchBo.getLimit(), true,true,true);
        //获取项目查询结果集
        List<Contract> projectList = contractMapper.pageContractList(contractSearchBo);
        return Objects.requireNonNull(R.ok().put(FieldConstant.TOTAL, page.getTotal())).setData(Optional.ofNullable(projectList).orElse(Lists.newArrayList()));
    }

    /**
     * 更新合同
     * @param contract 合同
     * @return VO
     */
    @Override
    public R updateContract(Contract contract) {
        // 已绑定项目的合同不可以更新项目id,除非解绑项目
        Contract c = getById(contract.getContractId());
        if (contract.getProjectId() == null || contract.getProjectId() == 0
                || c.getProjectId() == null || c.getProjectId() == 0) {
            updateById(contract);
            return R.ok();
        } else {
            if (contract.getProjectId().doubleValue() == c.getProjectId().doubleValue()) {
                updateById(contract);
                return R.ok();
            } else {
                return R.error("已绑定项目的合同不允许更新项目编码,否则请先解绑合同和项目关系");
            }
        }
    }

    /**
     * 删除合同
     * @param contractId 合同编码
     * @param userId 操作者编码
     * @return VO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateDelContract(Long contractId, Long userId) {
        // 已绑定项目的合同不可以直接删除
        Contract c = getById(contractId);
        if (c.getProjectId() == null || c.getProjectId().doubleValue() == 0) {
            contractMapper.updateDelContract(contractId, userId);
            return R.ok();
        } else {
            return R.error("已绑定项目的合同不可以直接删除,否则请先解绑合同和项目关系");
        }
    }

    /**
     * 查询合同列表
     * @param contractSearchBo 查询BO
     * @return VO
     */
    @Override
    public R selectContractList(ContractSearchBo contractSearchBo) {
        List<Contract> projectList = contractMapper.pageContractList(contractSearchBo);
        return R.ok().setData(Optional.ofNullable(projectList).orElse(Lists.newArrayList()));
    }

}